#include "layerconfigmanager.h"
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardPaths>
#include <QDebug>

LayerConfigManager::LayerConfigManager(QObject *parent) : QObject(parent)
{
    // 获取应用数据目录
    m_configDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + "layerConfigs";
    ensureConfigDirExists();
}

bool LayerConfigManager::saveConfig(const QString &name, const QList<MapLayer> &layers)
{
    // 创建配置文件
    QString filePath = m_configDir + QDir::separator() + name + ".json";
    QFile file(filePath);
    
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "无法打开配置文件进行写入:" << filePath;
        return false;
    }
    
    // 构建JSON数据
    QJsonObject configObj;
    configObj["name"] = name;
    
    QJsonArray layersArray;
    for (const MapLayer &layer : layers) {
        layersArray.append(layer.toJson());
    }
    configObj["layers"] = layersArray;
    
    // 写入文件
    QJsonDocument doc(configObj);
    file.write(doc.toJson(QJsonDocument::Indented));
    file.close();
    
    return true;
}

QList<MapLayer> LayerConfigManager::loadConfig(const QString &name)
{
    QList<MapLayer> layers;
    
    // 打开配置文件
    QString filePath = m_configDir + QDir::separator() + name + ".json";
    QFile file(filePath);
    
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "无法打开配置文件进行读取:" << filePath;
        return layers;
    }
    
    // 读取并解析JSON数据
    QByteArray data = file.readAll();
    file.close();
    
    QJsonDocument doc = QJsonDocument::fromJson(data);
    if (!doc.isObject()) {
        qWarning() << "配置文件格式错误:" << filePath;
        return layers;
    }
    
    QJsonObject configObj = doc.object();
    QJsonArray layersArray = configObj["layers"].toArray();
    
    for (const QJsonValue &value : layersArray) {
        if (value.isObject()) {
            layers.append(MapLayer::fromJson(value.toObject()));
        }
    }
    
    return layers;
}

QStringList LayerConfigManager::availableConfigs() const
{
    QStringList configNames;
    QDir configDir(m_configDir);
    
    if (!configDir.exists()) {
        return configNames;
    }
    
    // 获取所有.json文件
    QStringList filters; filters << "*.json";
    QStringList files = configDir.entryList(filters, QDir::Files);
    
    for (QString &file : files) {
        configNames.append(file.replace(".json", ""));
    }
    
    return configNames;
}

bool LayerConfigManager::deleteConfig(const QString &name)
{
    QString filePath = m_configDir + QDir::separator() + name + ".json";
    return QFile::remove(filePath);
}

QString LayerConfigManager::configDir() const
{
    return m_configDir;
}

void LayerConfigManager::ensureConfigDirExists()
{
    QDir dir(m_configDir);
    if (!dir.exists()) {
        dir.mkpath(".");
    }
}